;Text_Buffer is the 25 character buffer used to hold the line of text
;to display on a particular row.

;Start_Of_Word is the 2-byte location in the beginning of a word.
;Start_Of_Word does not have its value changed until a new word is reached.

;String_Location is the character currently being read.  2-Bytes.

;Length_Of_String is one byte, and is used to see if a particular line of text
;will exceed 96 pixels.

;Text_Buffer_Pointer is one byte.

;Text_Y_Coordinate is one byte.

;Previous_Line and Next_Line are each two bytes, pointing to the
;string location to go to when scrolling up or down
;Previous_Line_Temp is used for Previous_Line

;Start-up Code for reading text to display on the mission-briefing screen.

Cant_Move_Down .equ appbackupscreen + 72
Cant_Move_Up .equ appbackupscreen + 74

Sprite_Width .equ appbackupscreen + 100
Text_Buffer .equ appbackupscreen
Start_Of_Word .equ appbackupscreen + 50
String_Location .equ appbackupscreen + 52
Length_Of_String .equ appbackupscreen + 54

Text_Buffer_Pointer .equ appbackupscreen + 56
Text_Y_Coordinate .equ appbackupscreen + 58

Previous_Line .equ appbackupscreen + 60
Previous_Line_Temp .equ appbackupscreen + 62
Next_Line .equ appbackupscreen + 64

Start_Of_Line .equ appbackupscreen + 66

Stack_Pointer .equ appbackupscreen + 68
Stack_Pointer_2 .equ appbackupscreen + 70
Stack_For_Previous_Text .equ appbackupscreen + 700

Start:

	

Display_Cutscene:

;Search through the cutscene data for cutscene instructions.
;0 = Display Text with Character Portrait
;1 = Display Image as Overwrite.
;2 = Pause


	ld hl, Cutscene_Data

Search_For_Cutscene_Instructions:

	ld a, (hl)

	cp 1
	jr nz, _

	call Cutscene_Display_Image
	jr Move_To_Next_Cutscene_Byte

_

	cp 0
	jr nz, _

	








;Get ready to call the routine for displaying
	;scrolling text

	
	inc hl
	push hl


	ld ix, Portrait_Box

	ld a, 4
	ld (Sprite_Width), a

	ld b, 5
	ld l, 0
	xor a

	B_CALL Put_Sprite_Overwrite

	pop hl
	


	;Get the sprite data we need for a portrait.

	ld a, (hl)
	inc hl
	push hl

	;A sprite location takes two bytes.

	add a, a

	ld hl, Sprite_Table

	ld d, 0
	ld e, a
	add hl, de

	ld d, (hl)
	inc hl
	ld e, (hl)

	ex de, hl

;HL now points to the sprite.

	push hl
	pop ix	;IX contains the character portrait.

	ld a, 3
	ld (Sprite_Width), a

	ld b, 24
	ld l, 1
	ld a, 1


	pop hl
	

	call Display_Scrolling_Text

	jr Move_To_Next_Cutscene_Byte
_













	cp 2
	jr nz, _

	inc hl
	ld b, (hl)	;BC is the number of
			;120ths of a second to pause
	inc hl
	ld c, (hl)

	;We will always pause after displaying
	;sprites so the user has a chance to see
	;what is going on.

Repeat_Pause_Timer:

	halt
	dec c
	jr nz, Repeat_Pause_Timer
	djnz Repeat_Pause_Timer
	jr Move_To_Next_Cutscene_Byte


_









	cp 252
	jr nz, Move_To_Next_Cutscene_Byte

	bjump(_jforcecmdnochar)










Move_To_Next_Cutscene_Byte:

	inc hl
	jr Search_For_Cutscene_Instructions














	

Cutscene_Display_Image:

	inc hl	;The next four bytes are the following:
		;Width (In bytes), Height, X Position,
		;Y Position

	ld a, (hl)

	ld (Sprite_Width), a

	inc hl
	ld b, (hl)

	inc hl
	ld a, (hl)

	inc hl
	ld c, (hl)
	
	push hl	;Saves the position in our campaign data
	ld l, c

	pop ix

	call Put_Sprite_Overwrite

	push ix
	pop hl

	ret














Display_Scrolling_Text:

	ld (Stack_Pointer_2), sp

	ld de, Stack_For_Previous_Text
	ld (Stack_Pointer), de

	ld sp, (Stack_Pointer)

	ld d, h
	ld l, e

	push de
	ld (Stack_Pointer), sp
	ld sp, (Stack_Pointer_2)













	ld a, 1
	ld (Cant_Move_Up), a


	ld (Start_Of_Word), hl	;Start with the first word.
	ld (String_Location), hl
	ld (Start_Of_Line), hl

	xor a
	ld (Text_Buffer_Pointer), a ;Start the line of text at the beginning of the buffer

	ld (Length_Of_String), a	;We don't know how big a line of text is

	ld (Text_Y_Coordinate), a

	ld hl, Clear_Text_Buffer
	ld de, Text_Buffer
	ld bc, 25
	ldir

Read_Character_Of_Text:

	;Read a character.  We first check to see if the character will
	;cause the string to flow off of the edge of the screen.

	ld hl, (String_Location)

	;A is the character.

	ld a, (hl)

	cp 250	;End of string? 250 means the end of the text
		;to display

	;If the end of the string has be reached, we have no
	;need to try to display anything else.

	jp nz, _

	ld a, 1
	ld (Cant_Move_Down), a

	jp Get_Key

_


	;Find the number of pixels a character requires so that
	;we can see if the line of text is going to be too
	;big to fit on the screen

	call Add_To_Length_Of_String

	ld a, (Length_Of_String)

	cp 92	;Is our string too big for the screen?

	;If so, we shorten the screen so that we can safely display
	;one line of text

	jp nc, Cut_String_To_Fit_On_Screen


	;Check to see if the character is a space, meaning that
	;one word has ended

	ld hl, (String_Location)
	ld a, (hl)

	cp $2F	;End of Word

	jr nz, Add_Character_To_String_And_Move_To_Next_Character_To_Check

	;If the character is a space, we tell the calculator
	;that one word has ended and we need to start a new word

	ld c, a	;Save the fact that the character is a space

	ld hl, (String_Location)
	inc hl

	ld (Start_Of_Word), hl		;New word
	ld (String_Location), hl

	ld a, (Text_Buffer_Pointer)

	ld hl, Text_Buffer

	call Add_A_To_HL

	ld (hl), c

	ld hl, Text_Buffer_Pointer
	inc (hl)


	jr Read_Character_Of_Text	

Add_Character_To_String_And_Move_To_Next_Character_To_Check:

	ld a, (Text_Buffer_Pointer)

	ld hl, Text_Buffer

	call Add_A_To_HL

	;Now we place the character in the line of text to display.

	
	ex de, hl

	ld hl, (String_Location)
	ld a, (hl)
	ld (de), a

	
	inc hl	;Goes to the next character of the string
	ld (String_Location), hl

	ld hl, Text_Buffer_Pointer
	inc (hl)

	jp Read_Character_Of_Text

	
Cut_String_To_Fit_On_Screen:

	;We display the text that will fit on the screen,
	;and then move onto a new line of text.

	
	call Put_Line_Of_Text



	ld a, (Text_Y_Coordinate)

	or a

	call z, Create_Previous_Line

	ld hl, (Start_Of_Word)
	ld (String_Location), hl
	ld (Start_Of_Line), hl


	add a, 6

	cp 59

	jp nc, Get_Key	;Out of screen space, so can't
			;place more text

	;We can safely move to the next line of the screen.

	cp 6

	call z, Create_Next_Line


	ld a, (Text_Y_Coordinate)
	add a, 6

	ld (Text_Y_Coordinate), a

	


	jp Read_Character_Of_Text


Get_Key:

	ld a, (Text_Y_Coordinate)

	or a

	call z, Create_Previous_Line

	ld hl, (String_Location)
	ld (Start_Of_Word), hl

	call Put_Line_Of_Text


	ld hl, plotsscreen
	call safecopy
	xor a
	ld (Text_Y_Coordinate), a
_

	B_CALL _GrBufClr
	B_CALL _getKey

	cp kUp

	jr z, Key_Up

	cp kDown

	jr z, Key_Down

	cp kEnter

	jr z, Key_Enter

	jr -_


Key_Up:


	ld (Stack_Pointer_2), sp


	ld sp, (Stack_Pointer)

	pop de
	pop hl
	




	or a
	sbc hl, de
	add hl, de
	jr nz, _

	push hl
	push de

	ld (Stack_Pointer), sp	

	ld sp, (Stack_Pointer_2)

	jr -_

_

	ld (Stack_Pointer), sp	

	ld sp, (Stack_Pointer_2)

	xor a
	ld (Cant_Move_Down), a

	ld (Start_Of_Line), hl

	ld (String_Location), hl
	ld (Start_Of_Word), hl
_
	jp Read_Character_Of_Text

Key_Down:

	ld a, (Cant_Move_Down)
	or a
	jr nz, ---_


	xor a
	ld (Cant_Move_Up), a

	ld hl, (Next_Line)
	ld (String_Location), hl
	ld (Start_Of_Word), hl
	ld (Start_Of_Line), hl

	jp Read_Character_Of_Text

Key_Enter:

	;Scan for either a new paragraph, or the end of the text

	ld hl, (String_Location)
	

Scan_Text:

	ld a, (hl)

	cp 250
	jr z, _

	inc hl
	jr Scan_Text

_


	inc hl

	ld a, (hl)
	cp 251
	ret z
	ld (String_Location), hl
	ld (Start_Of_Word), hl
	ld (Start_Of_Line), hl

	ld de, Stack_For_Previous_Text
	ld (Stack_Pointer), de

	ld sp, (Stack_Pointer)

	push hl

	ld (Stack_Pointer), sp
	ld sp, (Stack_Pointer_2)

	xor a
	ld (Text_Y_Coordinate), a
	ld (Text_Buffer_Pointer), a
	ld (Cant_Move_Down), a

	jp Read_Character_Of_Text




Create_Next_Line:

	

	ld hl, (Start_Of_Line)

	ld (Next_Line), hl
	
	ret

Create_Previous_Line:

	ld (Stack_Pointer_2), sp



	ld sp, (Stack_Pointer)

	ld hl, (Start_Of_Line)
	
	push hl

	ld (Stack_Pointer), sp
	
	ld sp, (Stack_Pointer_2)

	ret



Clear_Text_Buffer:

 .db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0






Cutscene_Data:


 #include "Cutscene.asm"














Text:

.db "TRANSMITTING:::",250 

.db "GREETINGS./CORSON://I/AM/P","RESIDENT/SOLOMONE/ERRAN/OF/THE
ESEO://I/AM/INFORMING/YOU","/OF/OUR/DECISION/CONCERNING/THE/NON>AGGRESSION/AND/NON>INTERFERENCE/PACT/YOU/O","FFERED/US/DURING/OUR/LAST/COMMUNICATION:",250

.db "LET/ME/BEGIN/BY/INFORMING/YOU/THAT/WE/HAVE/BEEN/WRONGLY/ACCUSED/BY/THE/TOSONIAN/RACE://DESPITE/WHAT/YOU/AN","D/YOUR/PEOPLE/BELIEVE./CORSON./WE/ARE/IN/NO/WAY/JOYFUL/OVER/THE/ASSISS","INATION/OF/JASILIN://ON/THE/CONTRARY./WE/ARE/VERY/GRIEVED./AND/EVEN/ON/A/PLANET/AS/DISTANT/AS/EARTH./MANKIND/IS/MOURNING/OVER/SUCH/A/HEAVY/LOSS:/BECAUSE/E","VEN/THOUGH/TOSONIANS/AS/A/WHOLE/HAVE/HELD/PREJUDICE/AGAINST/US./JASILI","N/WAS/AS/MUCH/OF/A/COMPANION./A/GUIDE./A/HERO/AND/A/BENEFIT/TO/EARTH/A","S/ZHAS/CURRENTLY/IS://WE/DO/NOT/UNDERSTAND/WHAT/SHE/SAW/IN/US./NOR/DO/WE/FEEL/WE/DESERVED/SUCH/KINDNESS/AND/ATTENTION://YET/THE/FACT/REMAINS/THAT/WE/HAVE/LOST/A/DEAR","/FRIEND./AND/WE/STILL/HAVE/NOT/MOVED/ON/FROM/THE/PAIN/WE/ARE/FEELING/AS/A/RESULT/OF/HER/DEATH:",250

.db "HOWEVER./WE/HAVE/NOT/LET/","OUR/PERSONAL/FEELINGS/COULD/OUR/JUDGEMENT./AND/I/WILL/SAY/WITHOUT/HESITATION/","THAT/WE/HAVE/NO/REASON/TO/BELIEVE/THAT/OUR/BROTHERS./THE/PTALOIDS./ARE/RESPONSIBLE/FOR/SUCH/A/HORRIBLE/CRIME://I/HAVE/HEARD/THAT/THE/TOSONIAN/RACE/PLANS/TO/INVEST","AGATE/THE/MATTER./AND/WE/HUMANS/HAVE/NO/OBJECTION://BUT/I/AND/TH","E/REST/OF/MANKIND/WILL/NEVER/ALLOW/THE/TOSONIANS/TO/TURN/SUCH/AN/INVESTIGATION/INTO/A/HARMFUL/AND/TORTUROUS/INTERREGATION:",250

.db "AND/BECAUSE/WE/WISH/TO/PROTECT","/OUR/ALLIES/FROM/SUCH/AN/ACT/SHOULD/IT/OCCUR./IT/IS/MY/DUTY/TO/INFORM/Y","OU/THAT/WE/CANNOT/SIGN/YOUR/PACT./NOR/CAN/WE/AGREE/TO/STAND/BY/AND/DO/NOTHI","NG/JUST/TO/KEEP/OURSELVES/OUT/OF/HARM=S/WAY://WE/ARE/READY/TO/BACK/THE/PTALOID","S/AND/TO/PROTECT/THEM/IF/THEY/ARE/FACED/WITH/ANY/THREAT:",250

.db "I/DO/NOT/DOUBT/THAT/THIS/WIL","L/CAUSE/YOUR/FEELINGS/TOWARD/US/TO/WORSEN./BUT/I/WOULD/ADVISE/YOU/TO/CONSI","DER/WHAT/IS/BEST/FOR/YOU/AND/ALL/THE/TOSONIAN/PEOPLE/BEFORE/YOU/TAKE/ANY/FORM/OF/RASH/ACTION://THIS/IS/PRESIDENT/ERRAN./OVER/AND/OUT:",250,251



Sprite_Table:

 .dw Protrait_Data
 .dw Protrait_Data + (24*3)
 .dw Portrait_Data + (24*3)


Portrait_Data:

 

















Add_A_To_HL:

	add a,l
 	ld l,a
 	adc a,h    ;^ these two lines
	sub l      ;v increase h if there is carry
 	ld h,a
 
  	ret

;Routines for the custom S.A.D. font. These routines are used so the interrupt
;routine can run









Add_Character_Normal_Length:

	ld a, (Length_Of_String)
	add a, 4
	ld (Length_Of_String), a

	ld a, 1
	ret



Add_Character_Two_Length:

	ld a, (Length_Of_String)
	add a, 2
	ld (Length_Of_String), a

	ld a, 1
	ret

Add_Character_Six_Length:

	ld a, (Length_Of_String)
	add a, 6
	ld (Length_Of_String), a

	ld a, 1
	ret

Add_Character_Eight_Length:

	ld a, (Length_Of_String)
	add a, 8
	ld (Length_Of_String), a

	ld a, 1
	ret

Add_To_Length_Of_String:

	cp $3A
	call c, Add_Character_Normal_Length



	cp $4D
	call z, Add_Character_Six_Length

	cp $57
	call z, Add_Character_Six_Length

	cp 91
	call nc, Add_Character_Eight_Length
	
	cp $3E
	call nc, Add_Character_Normal_Length

	cp $3A
	call nc, Add_Character_Two_Length

	ret


Multiply_DE_By_A

	
	ld hl, 0		;hl will store the product
	ld b, 8			;check 8 bits
	
_

	rrca			;check bit 0 of register A
	jr nc, _

	add hl, de

_

	sla e
	rl d

	djnz --_
	ret

Put_Line_Of_Text:

	xor a
	ld (Text_Buffer_Pointer), a
	ld (Length_Of_String), a

	;Get to the correct location in Text_Buffer, and clear
	;the text afterwards so we don't have bad text at the
	;end of the line

	ld hl, (Start_Of_Word)
	ld de, (Start_Of_Line)

	or a
	sbc hl, de

	ld a, l


	
	ld hl, Text_Buffer
	call Add_A_To_HL


	ld de, Clear_Text_Buffer
	ex de, hl

	ld bc, 25

	ldir

	ld l, 0
	ld a, (Text_Y_Coordinate)
	ld h, a
	ld (penCol), hl

	ld hl, Text_Buffer

	call Put_Text_Zero_End

	ret
